<?php
namespace app\admin\controller;
use \think\Request;
use think\Db;
use app\common\model\Banner as BannerModel;
class Banner extends \app\admin\controller\Base {
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list'),
        'add'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
        'update'   => array('css' => '2,3,4,5,9', 'js' => 'ueditor_config,ueditor_all,ueditor_lang'),
        'save'   => array('css' => '2,3,4,5,9','js'=>'list')
    );
    public function index(){
        $BannerModel = new BannerModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $BannerModel->relData(15);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;


        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,

            )
        );
        return $this->fetch();
    }

    /**
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        $BannerModel = new BannerModel();
        if(Request::instance()->isPost()){
            if($_FILES['file']['name'][0]=='') {
                return $this->api_dbError("上传文件不能为空");
            }
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['file']['name'][0]);
            $filePath = $_FILES['file']['tmp_name'][0];
            $info = $this->qnCommon($rand,$exe[1],$filePath);
            if($info['status']!=200) {
                 return $this->api_dbError("上传文件格式不正确");
            }
            $data['banner_img'] =QiNiu.$info['key'];
            $data['banner_title'] = input('post.banner_title');
            $data['banner_sort'] = input('post.banner_sort');
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            if(input('post.banner_type') == 1){
                $data['banner_content'] = input('post.content');
            }else{
                $data['banner_href'] = input('post.link');
            }
            $data['banner_type'] = input('post.banner_type');
            $res = $BannerModel->save($data);
            if ($res) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        return $this->fetch();

    }

    public function update(){
        $BannerModel = new BannerModel();
        if(Request::instance()->isPost()){
            if($_FILES['file']['name'][0]!='') {
                $rand = date('YmdHis').rand(100000,999999);
                $exe = explode('.',$_FILES['file']['name'][0]);
                $filePath = $_FILES['file']['tmp_name'][0];
                $info = $this->qnCommon($rand,$exe[1],$filePath);
                if($info['status']!=200) {
                    return $this->api_dbError("上传文件格式不正确");
                }
                $data['banner_img'] =QiNiu.$info['key'];

            }
            $data['banner_id'] = input('post.id');
            $data['banner_title'] = input('post.banner_title');
            $data['banner_sort'] = input('post.banner_sort');
            $data['banner_time'] = date('Y-m-d H:i:s',time());
            if(input('post.banner_type') == 1){
                $data['banner_content'] = input('post.content');
            }else{
                $data['banner_href'] = input('post.link');
            }
            $data['banner_type'] = input('post.banner_type');
            $res = $BannerModel->update($data);
            if ($res) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        $id = input('get.id');
        $where['banner_id'] = $id;
        $list = $BannerModel->getOne($where);
        $this->assign('id', $id);
        $this->assign('ad', $list);
        return $this->fetch();

    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function delBanner(){
        $ids = strtoarray(input('post.ids'));
        $BannerModel = new BannerModel();
        $where['banner_id'] = array('In', $ids);
        $result = $BannerModel->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * @return \think\response\Json
     * 排序
     */
    public function sortBanner(){
        $sort = input('post.sort');
        $wine_id = input('post.wine_id');
        $BannerModel = new BannerModel();
        $where['banner_id'] = $wine_id;
        $res = $BannerModel->where($where)->setField("banner_sort",$sort);
        if ($res) {
            return $this->api_success("操作排序成功");
        } else {
            return $this->api_dbError("操作排序失败");
        }
    }

    /***
     * @return \think\response\Json
     */
    public function statusBanner(){
        $status = input('post.status');
        $act = input('post.act');
        $BannerModel = new BannerModel();
        $where['banner_id'] = $act;
        $res = $BannerModel->where($where)->setField("banner_status",$status);
        if ($res) {
            return $this->api_success("操作成功");
        } else {
            return $this->api_dbError("操作失败");
        }
    }

    /***
     * 类型图片
     */
    public function picture(){
        $page = input('get.page');
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $result = Db::table('up_advertising')->paginate(4);
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$result,
                'page'=>$page,

            )
        );
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 禁用和启用
     */
    public function pictureStatus(){
        $status = input('post.status');
        $act = input('post.act');
        $where['ad_id'] = $act;
        $res =  Db::table('up_advertising')->where($where)->setField("ad_status",$status);
        if ($res) {
            return $this->api_success("操作成功");
        } else {
            return $this->api_dbError("操作失败");
        }
    }

    /**
     * @return mixed
     * 编辑功能
     */
    public function save(){
        $id  = input('get.id');
        $res = Db::table('up_advertising')->where('ad_id',$id)->find();
        if(Request::instance()->isPost()){
            $fid = input('post.id');
            $ad_name = input('post.ad_name');
            $data['ad_name'] = $ad_name;
            $data['ad_time'] = date('Y-m-d H:i:s',time());
            if($_FILES['file']['name'][0]!=''){
                $rand = date('YmdHis').rand(100000,999999);
                $exe = explode('.',$_FILES['file']['name'][0]);
                $filePath = $_FILES['file']['tmp_name'][0];
                $info = $this->qnCommon($rand,$exe[1],$filePath);
                if($info['status']!=200) {
                    return $this->api_dbError("上传文件格式不正确");
                }
                $data['ad_img'] =QiNiu.$info['key'];
            }
            $result = Db::table('up_advertising')->where('ad_id',$fid)->update($data);
            if ($result) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        if($res['ad_id']){
            $this->assign('res',$res);
        }
        return $this->fetch();
    }
}